package api;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import lombok.SneakyThrows;
import util.AppVar;
import util.DBUtils;
import util.ResultUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;

@WebServlet("/delart")
public class DelArticleServlet extends HttpServlet {
    @SneakyThrows
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String msg="未知错误";
        int data=0;
        Integer aid=null;
        try {
            aid=Integer.valueOf(req.getParameter("aid"));
        }catch (Exception e){
        }
        if (aid!=null&&aid>0){ //参数有效
           //删除文章
            Connection connection= DBUtils.getConnection();
            String sql="delete from article where aid=?";
            PreparedStatement statement=connection.prepareStatement(sql);
            statement.setInt(1,aid);
            data=statement.executeUpdate();

            //关闭资源
            DBUtils.close(null,statement,connection);
            msg="";
        }else {
            msg="非法参数";
        }
        resp.setContentType(AppVar.RESPONSE_CONTENTTYPE_JSON);
        resp.getWriter().println(JSONUtil.toJsonStr(ResultUtils.toResult(200,data,msg)));
    }
}
